<template>
  <el-dialog v-model="verifyVisible" title="用户核销"
             :before-close="closeEditDialog"
             width="40%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
             label-width="160px" class="demo-ruleForm"
             :size="formSize" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="核销日期" prop="verifyMonth">
            <el-date-picker
                v-model="ruleForm.verifyMonth"
                type="month"
                style="width: 100%"
                @change="verifyMonthChange"
                format="YYYY-MM"
                value-format="YYYY-MM"
                placeholder="请选择核销月份"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
         <span class="dialog-footer">
           <el-button @click="closeEditDialog">关闭</el-button>
           <el-button type="primary"
                      @click="submitForm(ruleFormRef)">提交</el-button>
         </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage} from "element-plus";
import router from "@/router";
import {
  SysAdminVerify
} from '@/apis/model/order/SysAdminVerifyModel';
import {
  insertSysAdminVerify,
  operUserMonthVerify,
  updateSysAdminVerify
} from '@/apis/http/order/SysAdminVerifyHttp';
import {userStore} from "@/store";
const store = userStore();
const userInfo = store.userInfo;

interface Prop {
  verifyVisible: boolean,
}

// 刷新list页面
const refushList = ref(false);

const   ruleForm = ref({
  verifyType: 1,
  adminId: userInfo.id
} as SysAdminVerify);


// 传递过来的参数
const props = defineProps<Prop>();

const emit = defineEmits(["closeEditDialog"]);

// 关闭按钮
const closeEditDialog = () => {
  emit("closeEditDialog", {
    verifyVisible: !props.verifyVisible,
    refushList: refushList.value
  })
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  id: [
    {required: true, message: '请输入', trigger: 'blur'}
  ],
  verifyTime: [
    {required: true, message: '请输入核销时间', trigger: 'blur'}
  ],
  adminId: [
    {required: true, message: '请输入用户名称', trigger: 'blur'}
  ],
  adminLevel: [
    {required: true, message: '请输入用户等级', trigger: 'blur'}
  ],
  orderCount: [
    {required: true, message: '请输入总单子数量', trigger: 'blur'}
  ],
  ownerOrderCount: [
    {required: true, message: '请输入自己的单子', trigger: 'blur'}
  ],
  totalPrice: [
    {required: true, message: '请输入总金额', trigger: 'blur'}
  ],
  ownerOrderPrice: [
    {required: true, message: '请输入自己单子金额', trigger: 'blur'}
  ],
  totalVerifPrice: [
    {required: true, message: '请输入核销总金额', trigger: 'blur'}
  ],
  totalOwnerVerifPrice: [
    {required: true, message: '请输入自己单子核销金额', trigger: 'blur'}
  ],
  wechatPer: [
    {required: true, message: '请输入提现手续费', trigger: 'blur'}
  ],
  wechatPrice: [
    {required: true, message: '请输入提现金额', trigger: 'blur'}
  ],
  examineVerify: [
    {
      required: true,
      message: '请输入审核单子核销状态 0-待审核 1-审核通过 2-审核不通过',
      trigger: 'blur'
    }
  ],
  tixianFlag: [
    {required: true, message: '请输入提现状态 0-待提现 1-已提现 2-提现失败', trigger: 'blur'}
  ],
  statusFlag: [
    {required: true, message: '请输入状态 0-停用 1-启用 2-备用', trigger: 'blur'}
  ],
  delFlag: [
    {required: true, message: '请输入是否删除 0-否 1-是', trigger: 'blur'}
  ],
  createUser: [
    {required: true, message: '请输入创建人', trigger: 'blur'}
  ],
  createTime: [
    {required: true, message: '请输入创建时间', trigger: 'blur'}
  ],
  updateTime: [
    {required: true, message: '请输入修改时间', trigger: 'blur'}
  ],
  remark: [
    {required: true, message: '请输入备注', trigger: 'blur'}
  ],
})

const verifyMonthChange = (e: any) => {
  ruleForm.value.verifyMonth = e;
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 新增
      operUserMonthVerify(ruleForm.value).then(res => {
        const code = res.code;
        const message: string = res.message;
        const data = res.data;
        if (code == 200) {
          ElMessage({
            message: '正在核算中，请稍后...',
            type: 'success',
          })
          refushList.value = true
          closeEditDialog()
        } else {
          ElMessage.error('核算失败！')
        }
      })
    } else {
      ElMessage.error('核算失败！')
    }
  })
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
